package mobi.richaze.llbag.infrastructure.hibernate;

import java.util.List;

import mobi.richaze.llbag.domain.model.account.Account;
import mobi.richaze.llbag.domain.model.account.AccountRepository;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

@Repository("AccountRepositoryHibernate")
public class AccountRepositoryHibernate extends HibernateRepository<Account>
		implements AccountRepository {

	private static final Logger log = LoggerFactory
			.getLogger(AccountRepositoryHibernate.class);

	public Account getAccount(String username) throws DataAccessException {
		return null;
	}

	public Account getAccount(String username, String password)
			throws DataAccessException {
		log.debug("getting Account instance with username:" + username
				+ " and password:" + password);
		try {

		} catch (Exception e) {
			log.error("get failed", e);
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<Account> getUsernameList() throws DataAccessException {
		log.debug("getting Account instances");
		List<Account> list = null;
		try {
			Criteria criteria = getSession().createCriteria(Account.class,
					"scheme");
			list = criteria.list();
			log.debug("get Account instances successful, result size: "
					+ list.size());
		} catch (Exception ex) {
			log.error("get failed", ex);
			ex.printStackTrace();
		}

		return list;
	}

	public void insertAccount(Account account) throws DataAccessException {

	}

	public void updateAccount(Account account) throws DataAccessException {

	}

	@Autowired
	public void setSessionFactorys(SessionFactory sessionFactory) {
		super.setSessionFactory(sessionFactory);
	}

	private static ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
			new String[] { "classpath:applicationContext.xml" });

	public static void main(String[] args) {
		AccountRepositoryHibernate accountRepository = (AccountRepositoryHibernate) ctx
				.getBean("AccountRepositoryHibernate");
		Account entity = new Account();
		entity.setUserid("haha_61");
		accountRepository.removeEntity("haha_6");
	}

}
